package com.resume.controller

import com.resume.common.Result
import com.resume.dto.LoginRequest
import com.resume.dto.RegisterRequest
import com.resume.service.UserService
import org.springframework.web.bind.annotation.*
import jakarta.validation.Valid

@RestController
@RequestMapping("/api/auth")
@CrossOrigin(origins = ["http://localhost:3000"])
class AuthController(
    private val userService: UserService
) {
    
    @PostMapping("/login")
    fun login(@Valid @RequestBody request: LoginRequest): Result<Any> {
        return try {
            val result = userService.login(request.username, request.password)
            Result.success(result)
        } catch (e: Exception) {
            Result.error(e.message ?: "登录失败")
        }
    }
    
    @PostMapping("/register")
    fun register(@Valid @RequestBody request: RegisterRequest): Result<Any> {
        return try {
            val result = userService.register(request)
            Result.success(result)
        } catch (e: Exception) {
            Result.error(e.message ?: "注册失败")
        }
    }
    
    @PostMapping("/logout")
    fun logout(@RequestHeader("Authorization") token: String): Result<Any> {
        return try {
            userService.logout(token)
            Result.success("退出成功")
        } catch (e: Exception) {
            Result.error(e.message ?: "退出失败")
        }
    }
} 